// Created by lifei on 2020/7/20--11:17.
import pager from "@/assets/js/page-mixin";
import {
    searchUnitList,
    exportUnitList,
    deleteUnitList
} from "@/views/authorization-management/apply-unit/api/apply-unit.js";
import applyUnitAdd from "~/views/authorization-management/apply-unit/window/unit-add.vue";
import applyUnitEdit from "~/views/authorization-management/apply-unit/window/unit-edit.vue";
import applyUnitDetail from "~/views/authorization-management/apply-unit/window/unit-detail.vue";
import unitApplyList from "~/views/authorization-management/apply-unit/window/unit-property-list.vue";
import { getDict } from "~/utils";
import { mapGetters } from "vuex";

export default {
    name: "resource-manage",
    mixins: [pager],
    provide() {
        return {
            initList: this.query,
        };
    },
    beforeMount() {
        this.add_but = this.permissions["add-unit"];
        this.update_but = this.permissions["update-unit"];
        this.delete_but = this.permissions["delete-unit"];
        this.query();
    },
    components: {
        applyUnitAdd: applyUnitAdd,
        applyUnitEdit: applyUnitEdit,
        applyUnitDetail: applyUnitDetail,
        unitApplyList: unitApplyList
    },
    data() {
        return {
            params: {
                current: 1,
                size: 10,
                keyword: ''
            },
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 10 // 每页显示多少条
            },
            applyUnitType: [],
            multipleSelection: [],//表格选中行对象数组
            loading: true,
            //表单数据
            tableData: [],

            // 传入过滤条件里面的内容
            //按钮权限
            add_but: false,
            update_but: false,
            delete_but: false,
            filterList: [
                {
                    label: "单位类型",

                    inputType:'dict',
                    dictName:'apply-unit-type',
                    name: "unitTypeList",
                    children: []
                },
                {
                    label: "申请时间",
                    inputType: 'daterange',
                    name:['applyTimeStart','applyTimeEnd']
                },
                {
                    label: "联系人名称",
                    type:'input',
                    name: "userName"
                },
                {
                    label: "联系电话",
                    type: "input",
                    name: "tell"
                }
            ],

        };
    },
    computed: {
        ...mapGetters(["permissions"])
    },
    methods: {
        // 菜单点击监听
        handleMenuClick(type){
            switch (type) {
                case 'add':
                    this.$refs.applyUnitAdd.show();
                    break;
                case 'export':
                    this.exportUnit()
                    break;
            }
        },

        handleClick(item) {
            this.$router.push(
                "/property-info-sys/property-info-manage/resource-manage/edit"
            );
        },

        /**
         * 查询方法
         * @param param
         */
        query(params={}) {
            this.loading = true;
            let req  =  {...this.params,...params}
            //alert(JSON.stringify(req));
            searchUnitList(req)
                .then(res => {
                    let {size,current,total,records} = res.data.data
                    this.tableData=records
                    this.params=req
                    this.pager = {size,current,total}
                })
                .catch(error => {
                    this.$message({
                        type: "error",
                        message: "查询失败"
                    });
                })
                .finally(() => (this.loading = false));
        },

        /**
         * 列表导出
         */
        exportUnit() {
            exportUnitList(this.searchKeyword, 1, 10000).then(res => {
                // 关闭loading
                this.loading = false;
                // 此处有个坑。这里用content保存文件流，最初是content=res，但下载的test.xls里的内容如下图1，
                // 检查了下才发现，后端对文件流做了一层封装，所以将content指向res.data即可
                // 另外，流的转储属于浅拷贝，所以此处的content转储仅仅是便于理解，并没有实际作用=_=
                const content = res.data;
                const blob = new Blob([content]); // 构造一个blob对象来处理数据
                const fileName = "使用单位列表.xls"; // 导出文件名
                // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
                // IE10以上支持blob但是依然不支持download
                if ("download" in document.createElement("a")) {
                    // 支持a标签download的浏览器
                    const link = document.createElement("a"); // 创建a标签
                    link.download = fileName; // a标签添加属性
                    link.style.display = "none";
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link);
                    link.click(); // 执行下载
                    URL.revokeObjectURL(link.href); // 释放url
                    document.body.removeChild(link); // 释放标签
                } else {
                    // 其他浏览器
                    navigator.msSaveBlob(blob, fileName);
                }
                this.btnSendTem = false;
            });
        },

        /**
         * 删除方法
         * @param scope
         */
        deleteRow(scope) {
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    // 删除数据
                    deleteUnitList(scope.row.apUiId)
                        .then(response => {
                            let mes = response.data.msg;
                            this.query();
                            this.$message({
                                type: "success",
                                message: "操作成功"
                            });
                        })
                        .catch(error => {
                            this.$message({
                                type: "error",
                                message: "操作失败"
                            });
                        })
                        .finally(() => (this.loading = false));
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        },

        /**
         * 打开编辑弹窗
         * @param scope
         */
        toWindowEditData(scope) {
            this.$refs.applyUnitEdit.show(scope.row);
        },

        /**
         * 查看
         * @param scope
         */
        toWindowDetailData(scope) {
            this.$refs.applyUnitDetail.show(scope.row);
        },

        /**
         * 物业信息
         * @param scope
         */
        toWindowApplyList(scope) {
            this.$refs.unitApplyList.show(scope.row);
        },
        /**
         * 码值转换
         * @param row
         * @param column
         * @param cellValue
         * @returns {string|*}
         */
        dictVal_unitType: function(row, column, cellValue) {
            let obj = getDict("apply-unit-type").find(p => p.value == cellValue);
            if (obj) {
                return obj.label;
            }
            return "";
        }
    }
};
